<?
  require_once('../db.php');
  require_once('../util/polypack.php');
  require_once('../util/geometry_to_array.php');
  db_connect();

  $query = 'SELECT name_CNT AS name, code_CNT AS code, AsText(boundary_CNT) AS boundary FROM ldi_county_cnt ORDER BY order_CNT';
  $result = mysql_query($query) or die("ERROR PERFORMING QUERY '".$query."': ".mysql_error());

  $counties = array();
?>

var county_polygons_enc = {<?
  $first = true;
  while ($row = mysql_fetch_assoc($result)) {
    $counties[$row["code"]] = $row["name"];
    if ($first)
      $first = false;
    else
      echo ",";
    echo "\n\n";
    echo $row["code"].": [";
    $b = $row["boundary"];
    $p = geometry_to_array($b, true);
    $c = count($p);
    for ($i=0; $i<$c; $i++) {
      if ($i > 0)
        echo ",\n\n";
      echo $p[$i];
    }
    echo "]";
  }

  mysql_free_result($result);
?>


};

var county_names = {
<?
  $first = true;
  foreach ($counties as $k=>$v) {
    if ($first)
      $first = false;
    else
      echo ", ";
    echo $k.": '".$v."'";
  }
?>

};

var county_polygons = {};
for (var p in county_polygons_enc) {
  var poly = county_polygons_enc[p];
  var poly_decoded = [];
  for (var i=0; i<poly.length; i++) {
    var s = decode_polygon(poly[i]);
    poly_decoded.push(s);
  }
  county_polygons[p] = poly_decoded;
}

loadPolyCluster();
